package com.bos.web.action;
import com.bos.domian.base.BaseQuery;
import com.bos.domian.base.Promotion;
import com.bos.domian.constant.Constants;
import com.bos.web.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import javax.ws.rs.core.MediaType;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author misterWei
 * @create 2018年11月21号:10点25分
 * @mailbox mynameisweiyan@gmail.com
 */
@ParentPackage("json-default")
@Namespace("/")
@Actions
@Controller
@Scope("prototype")
public class PromotionAction  extends BaseAction<Promotion>{

    @Action(value = "promotion_PageQuery",results = {@Result(type = "json")})
    public String promotionPageQuery(){
        BaseQuery baseQuery = WebClient.create(Constants.BOS_MANAGER_PORT+"/services/promotionService/pageQuery?page=" + page + "&rows=" + rows).encoding("utf8").accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).get(BaseQuery.class);
        ActionContext.getContext().getValueStack().push(baseQuery);
        return SUCCESS;
    }
   @Action(value = "promotion_detail")
    public String promotionDetail(){
       System.out.println(model.getId());
        //转码操作
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
       //获取当前绝对路径下
        String realPath = ServletActionContext.getServletContext().getRealPath("/templates/");
        //判断当前下的静态资源是否存在,
        File file = new File(realPath+"/"+model.getId()+".html");
        try {
            //如果不存在
            if (! file.exists()) {
                //不存在需要创建静态资源,将详细数据进行填充
                Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
                //静态模板存储的文件夹
                cfg.setDirectoryForTemplateLoading(new File(ServletActionContext.getServletContext().getRealPath("/freemaker/")));
                cfg.setDefaultEncoding("utf-8");
                //获取的静态模板
                Template template = cfg.getTemplate("promostion_detail.ftl");
                template.setEncoding("utf-8");
                //进行数据的填充
                Promotion promotion = WebClient.create(Constants.BOS_MANAGER_PORT + "/services/promotionService/findOne/"+model.getId()).encoding("utf-8").get(Promotion.class);

                Map<String,Object> promostionMap  = new HashMap<>();
                promostionMap.put("promostion",promotion);

                //最后输出生成静态文件
                 template.process(promostionMap, new FileWriter(file));
            }
            //如果存在 将这个文件读取出来将内容返回到客户端
            FileUtils.copyFile(file,ServletActionContext.getResponse().getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return NONE;
    }
}
